package com.bolt.support.spring.autoconfigure;

import com.bolt.common.BoltConstants;
import com.bolt.common.collection.IteratorUtil;
import com.bolt.support.spring.mvc.bean.ResourceProperties;
import com.bolt.support.spring.mvc.method.ModelEntityMethodProcessor;
import com.bolt.support.spring.mvc.resolver.CustomPageableResolver;
import com.bolt.support.spring.mvc.resolver.CustomSortResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * Created by Administrator on 2019/10/29.
 */
@Configuration
@EnableConfigurationProperties({ResourceProperties.class})
public class WebAutoConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private ResourceProperties resourceProperties;

    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(getModelEntityMethodProcessor());
        // 注册Spring data jpa pageable的参数分解器
        CustomPageableResolver pageableHandlerMethodArgumentResolver = new CustomPageableResolver();
        pageableHandlerMethodArgumentResolver.setPageParameterName(BoltConstants.PAGE_CURRENT_KEY);
        pageableHandlerMethodArgumentResolver.setSizeParameterName(BoltConstants.PAGE_SIZE);
        pageableHandlerMethodArgumentResolver.setOneIndexedParameters(false);
        CustomSortResolver customSortResolver = new CustomSortResolver();
        argumentResolvers.add(pageableHandlerMethodArgumentResolver);
        argumentResolvers.add(customSortResolver);

    }


    @Bean
    public ModelEntityMethodProcessor getModelEntityMethodProcessor() {
        return new ModelEntityMethodProcessor();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/").setCachePeriod(0);

        if (IteratorUtil.isNotEmpty(resourceProperties.getResources())) {
            for (ResourceProperties.ResourceConfig resourceConfig : resourceProperties.getResources()) {
                registry.addResourceHandler(resourceConfig.getPattern()).addResourceLocations(resourceConfig.getType() + ":" + resourceConfig.getPath());
            }
        }
        // registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
        super.addResourceHandlers(registry);
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }

    @Autowired(required = false)
    private MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;



    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        converters.add(0, jackson2HttpMessageConverter);

    }


}

 